package com._21cn._admin.utils;

import com._21cn.cms.common.Constants;
import com._21cn.framework.http.HttpRequestInfo;
import com._21cn.framework.http.HttpResponseUtil;
import com._21cn.framework.utils.StringUtil;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Administrator on 2017/9/9.
 */

@Component("adminAuthHelper")
public class AdminAuthHelper {

    public boolean isLogin( HttpRequestInfo reqInfo ){
        String cookieValue=reqInfo.getCookieValue(Constants.ADMIN_AUTH_KEY, "");
        if(StringUtil.isEmpty(cookieValue)){
            return false;
        }else {
            if(cookieValue.equals(Constants.ADMIN_AUTH_VALUE)){
                return true;
            }else {
                return false;
            }
        }
    }

    public void loginSetting(HttpServletRequest request, HttpServletResponse response){
        String domain = request != null? request.getServerName():  Constants.ADMIN_DOMAIN_NAME;
        String parts[] = domain.split( "\\." );
        String rootDomain = "."+parts[parts.length-2] +"."+ parts[parts.length-1];
        HttpResponseUtil.setCookie(response, Constants.ADMIN_AUTH_KEY, Constants.ADMIN_AUTH_VALUE, rootDomain, "/");
    }
}
